package com.wiscamp.ninechapters.common.core;


import jakarta.validation.constraints.NotBlank;

import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Function;

/**
 * Collection Helper Class
 *
 * @author yifeng
 */
public class CollectionHelper {
    private CollectionHelper() {
    }

    public static <T1, T2> Collection<T1> convertToArrayList(Collection<T2> source) {
        var target = new ArrayList<T1>();
        for (var element : source) {
            var targetElement = (T1) element;
            target.add(targetElement);
        }
        return target;
    }

    public static <T1, T2> Collection<T2> convertToArrayList(@NotBlank Collection<T1> source, Function<T1, T2> convertFunction) {
        Collection<T2> targetList = new ArrayList<>();
        for (var element : source) {
            var targetElement = convertFunction.apply(element);
            targetList.add(targetElement);
        }
        return targetList;
    }
}
